package com.example.seach;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<String> data;
    private String searchTxt = "";
    private List<String> dataBak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String txt="最近，无论在北上广深，还是四线小城，剧本杀开始兴起，|一般每场玩家5到8人，单局时长4到7小时。" +
                "|剧本杀的推理性、悬疑性可以满足玩家的表演欲，" +
                "还可以满足“侦探”玩家的推理爱好。|此外，剧本杀也为不少有社交需求玩家提供了平台。";
        String[] txts=txt.split("\\|");
        data = Arrays.asList(txts);
        dataBak = this.data;
        EditText et = findViewById(R.id.seach);

        ListView lv = findViewById(R.id.lv);
        final MyAdapter myAdapter = new MyAdapter();


        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchTxt = s.toString();
                String[] sp = searchTxt.split(" ");
                //循环遍历整个数据
                List<String> res = new ArrayList<>();
                for (String t : dataBak){
                    boolean found = true;
                    for (String x : sp){
                        if (x.length()==0 || x.isEmpty()){
                            continue;
                        }
                        //如果不包含搜索词
                        if (!t.contains(x)){
                            found = false;
                            break;
                        }
                    }
                    //筛选后的搜索词
                    if (found){
                        res.add(t);
                    }

                }

                data = res;
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        lv.setAdapter(myAdapter);

    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView = getLayoutInflater().inflate(R.layout.cell,null,false);
            }
            String txt = data.get(position);
            TextView title = convertView.findViewById(R.id.title);
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(txt);
            String[] key = searchTxt.split(" ");
            for (String k : key){
                int idx = txt.indexOf(k);
                ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
                spannableStringBuilder.setSpan(span,idx,idx+k.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
            title.setText(spannableStringBuilder);
            return convertView;
        }
    }
}